package com.ruoyi.project.mobile.user;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.feedback.domain.Feedback;
import com.ruoyi.project.system.feedback.service.IFeedbackService;
import com.ruoyi.project.system.goods.follow.dto.GoodsFollowDto;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberExamine;
import com.ruoyi.project.system.member.enums.MemberIsVip;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.order.order.enums.OrderStatus;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/24 16:25
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController {

	@Autowired
	private IMemberService memberService;

	@Autowired
	private IOrderOrderService orderOrderService;

	@Autowired
	private IGoodsGoodsService goodsGoodsService;

	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private ServerConfig serverConfig;
	@Autowired
	private IFeedbackService feedbackService;

	/**
	 * 点击会员中心 查询用户信息
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R findUser(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();

		Member oldMember = this.memberService.selectMemberById(uid);


		if(oldMember==null){
			return R.ok();
		}

		//判断用户会员是否过期
		if(oldMember.getIsVip().equals(MemberIsVip.YES.getValue())){
			if(DateUtils.getNowDate().getTime()>oldMember.getVipEndTime().getTime()){
				Member update = new Member();
				update.setIsVip(MemberIsVip.NO.getValue());
				update.setId(oldMember.getId());
				this.memberService.updateMember(update);
				oldMember.setIsVip(MemberIsVip.NO.getValue());
			}
		}


		map.put("user",oldMember);




		map.put("haoqiCoin",838);
		map.put("recommendNum",59);
		//待支付订单数量
		Integer[] toPaid = {OrderStatus.TO_BE_PAID.getValue()};
		map.put("toPayNum",this.orderOrderService.findListByStatusAndUid(toPaid,uid,0).size());
		//待发货订单数量
		Integer[] toDelivered = {OrderStatus.TO_BE_DELIVERED.getValue()};
		map.put("toDelivered",this.orderOrderService.findListByStatusAndUid(toDelivered,uid,0).size());
		//待收货订单数量
		Integer[] toReceived = {OrderStatus.TO_BE_RECEIVED.getValue()};
		map.put("toReceived",this.orderOrderService.findListByStatusAndUid(toReceived,uid,0).size());
		//待评论订单数量
		Integer[] toComment = {OrderStatus.TO_BE_COMMENT.getValue()};
		map.put("toComment",this.orderOrderService.findListByStatusAndUid(toComment,uid,0).size());
		//退款售后订单数量
//		Integer[] rxOrder = {OrderStatus.TO_BE_RETURN.getValue(),OrderStatus.TO_BE_EXCHANGE.getValue()};
		map.put("rxOrder",this.orderOrderService.findListByStatusAndUid(null,uid,1).size());
		return RFactory.generateR(map);
	}

	/**
	 * 查询关注列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/follows")
	public R userFollows(@RequestParam String uid){
		startPage();
		List<GoodsFollowDto> goodsFollowDtoList = this.goodsFollowService.findListByUid(uid);
		return RFactory.generateR(getDataTable(goodsFollowDtoList));
	}

	/**
	 * 查询用户详细信息
	 * @param uid
	 * @return
	 */
	@GetMapping("/info")
	public R userInfo(@RequestParam String uid){
		return RFactory.generateR(this.memberService.selectMemberById(uid));
	}



	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info")
	public R updateUserInfo(@RequestBody Map map){
		String uid = (String) map.get("uid");
		String name = (String) map.get("name");
		String idCard = (String) map.get("idCard");
		String mobile = (String) map.get("mobile");
		String header = (String) map.get("headerImg");
		String back = (String) map.get("backImg");
		Member member = new Member();
		member.setId(uid);
		member.setName(name);
		member.setIdCard(idCard);
		member.setMobile(mobile);
		member.setIsExamine(MemberExamine.ADOPT.getValue());
		member.setCardHeaderImg(header);
		//上传身份证背面的图片
		member.setCardBackImg(back);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 查询推荐人列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/recommend")
	public R userRecommend(@RequestParam String uid){
		startPage();
		Member member = new Member();
		member.setIntroUser(uid);
		List<Member> members = this.memberService.selectMemberList(member);
		return RFactory.generateR(getDataTable(members));
	}

	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info2")
	public R updateUserInfo2(@RequestBody Map map){
		String username = (String) map.get("username");
		String id = (String) map.get("id");
		int sex = (int) map.get("sex");
		String headpic = (String) map.get("headpic");
		Member member = new Member();
		member.setId(id);
		member.setUsername(username);
		member.setSex(sex);
		member.setHeadpic(headpic);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 提交意见
	 * @param feedback
	 * @return
	 */
	@PostMapping("/feedback")
	public R userFeedback(@RequestBody Feedback feedback){
		feedback.setCreated(DateUtils.getNowDate());
		this.feedbackService.insertFeedback(feedback);
		return R.ok();
	}

}
